﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using League.Entity;
using System.Data.SqlClient;
using System.Data;
using League.DBUtility;

namespace League.DataAccess
{
    public class LeagueTeamDataDataAccess
    {
        public static Guid Insert(params LeagueTeamData[] teamDataSet)
        {
            Guid result = Guid.Empty;
            foreach (LeagueTeamData leagueTeamData in teamDataSet)
            {
                SqlParameter[] parms = { 
                                            new SqlParameter("MatchID",leagueTeamData.MatchID),
                                            new SqlParameter("TeamID", leagueTeamData.TeamID),
                                            new SqlParameter("IsAtHome", leagueTeamData.IsAtHome),
                                            new SqlParameter("Goal",leagueTeamData.Goal),
                                            new SqlParameter("Shot",leagueTeamData.Shot),
                                            new SqlParameter("ShotOnTarget",leagueTeamData.ShotOnTarget),
                                            new SqlParameter("BlockedShot",leagueTeamData.BlockedShot),
                                            new SqlParameter("Corner",leagueTeamData.Corner),
                                            new SqlParameter("FreeKick",leagueTeamData.FreeKick),
                                            new SqlParameter("OutOfBounds",leagueTeamData.OutOfBounds),
                                            new SqlParameter("Foul",leagueTeamData.Foul),
                                            new SqlParameter("Offside",leagueTeamData.Offside),
                                            new SqlParameter("BallControl",leagueTeamData.BallControl),
                                            new SqlParameter("PassSuccess",leagueTeamData.PassSuccess),
                                            new SqlParameter("PassMiddleSuccess",leagueTeamData.PassMiddleSuccess),
                                            new SqlParameter("TacklingSuccess",leagueTeamData.TacklingSuccess),
                                            new SqlParameter("HeadSuccess",leagueTeamData.HeadSuccess),
                                            new SqlParameter("YellowCard",leagueTeamData.YellowCard),
                                            new SqlParameter("RedCard",leagueTeamData.RedCard)
                                       };
                LeagueSQLHelper.ExecuteScalar(CommandType.StoredProcedure,
                    "LeagueTeamDataInsert", parms);

                //string sql = string.Format("EXEC LeagueTeamDataInsert N'{0}',N'{1}',{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17}",
                //               leagueTeamData.MatchID, leagueTeamData.TeamID, leagueTeamData.IsAtHome, 
                //               leagueTeamData.Goal, leagueTeamData.Shot,
                //               leagueTeamData.ShotOnTarget, leagueTeamData.BlockedShot, leagueTeamData.Corner,
                //               leagueTeamData.FreeKick, leagueTeamData.OutOfBounds, leagueTeamData.Foul,
                //               leagueTeamData.Offside, leagueTeamData.BallControl, leagueTeamData.PassSuccess,
                //               leagueTeamData.PassMiddleSuccess, leagueTeamData.TacklingSuccess,
                //               leagueTeamData.YellowCard, leagueTeamData.RedCard);
                //LeagueSQLHelper.ExecuteScalar(CommandType.Text, sql);
            }
            return result;
        }

        public static List<LeagueTeamData> Find(string leagueMatchId)
        {
            string sql = string.Format("SELECT * FROM LeagueTeamData WHERE MatchID = '{0}'", leagueMatchId);
            return LeagueSQLEntityHelper<LeagueTeamData>.GetEntitySet(sql);
        }

        public static DataTable TeamHistory(string TeamId)
        {
            string sql = string.Format("SELECT TOP 20 i.LeagueName,m.MatchDateTime,m.MatchResult," +
                            " t.TeamName,h.TeamName AS HomeTeam,v.TeamName AS VisitingTeam,d.* " +
                            " FROM LeagueTeamData d " +
                            " LEFT JOIN LeagueMatch m ON d.MatchID=m.MatchID " +
                            " LEFT JOIN LeagueTeam t ON d.TeamID=t.TeamID " +
                            " LEFT JOIN LeagueTeam h ON h.TeamID=m.HomeTeamID " +
                            " LEFT JOIN LeagueTeam v ON v.TeamID=m.VisitingTeamID " +
                            " LEFT JOIN LeagueInfo i ON m.LeagueID=i.LeagueID " +
                            " WHERE d.TeamID='{0}' ORDER BY m.MatchDateTime DESC", TeamId);
            return LeagueSQLEntityHelper<LeagueTeamData>.GetDataTable(sql);
        }
    }
}
